package com.example.infowall02.mapper;

import com.example.infowall02.domain.Articles;
import com.example.infowall02.domain.LikeRequest;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;

public interface LikeMapper {
//增
    @Insert("INSERT INTO `like` (user_id, article_id) VALUES (#{userId}, #{articleId})")
    int addLike(@Param("userId") int userId, @Param("articleId") int articleId);
//删
    @Delete("DELETE FROM `like` WHERE user_id = #{userId} AND article_id = #{articleId}")
    int  deleteLike(@Param("userId") int userId, @Param("articleId") int articleId);
//查
    // 获取用户总点赞数
    @Select("SELECT COUNT(*) FROM `like` WHERE user_id = #{userId}")
    int countUserLikes(@Param("userId") int userId);
    // 检查用户是否对文章点赞
    @Select("SELECT COUNT(*) FROM `like` WHERE user_id = #{userId} AND article_id = #{articleId}")
    boolean countLikes(@Param("userId") int userId, @Param("articleId") int articleId);
    // 获取用户点赞的文章列表
    @Select("SELECT article_id AS id, type, theme, rule, date, location, sign_method as signMethod, origin, likes, clicks, favorites, image, content FROM articles" +
            " where article_id IN (SELECT article_id FROM `like` WHERE user_id = #{userId})"+
            " order by id desc"
    )
    List<Articles> getUserLikedArticles(@Param("userId") int userId);
    // 获取用户点赞列表
    @Select("SELECT like_id as likeId , user_id as userId , article_id as articleId FROM `like` WHERE user_id = #{userId} AND article_id = #{articleId}")
    LikeRequest getLike(int userId, int articleId);
}

